package edu.jd.mainframe;

import edu.jd.aflib.AfColumnLayout;
import edu.jd.login.LoginService;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginDialog extends JDialog {
    JButton ok = new JButton("登录");
    private boolean isOK = false;
    JTextField userIdTF = new JTextField(24);
    JTextField passwordTF = new JTextField(24);

    private LoginService loginService = new LoginService();

    public LoginDialog(){}

    public LoginDialog(JFrame owner){
        super(owner,"登录界面",true);
        showing();
    }

    private void showing(){
        this.setSize(300, 200);
        JPanel root = new JPanel();
        this.setContentPane(root);
        root.setLayout(new BorderLayout());
        JPanel main = new JPanel();
        main.setLayout(new AfColumnLayout());
        root.add(main, BorderLayout.CENTER);

        JPanel pUserId = new JPanel();
        pUserId.setLayout(new FlowLayout());
        pUserId.add(new JLabel("账号"));
        pUserId.add(userIdTF);
        main.add(pUserId);

        JPanel pPassword = new JPanel();
        pPassword.setLayout(new FlowLayout());
        pPassword.add(new JLabel("密码"));
        pPassword.add(passwordTF);
        main.add(pPassword);

        root.add(ok, BorderLayout.PAGE_END);

        ok.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent a){
                boolean isSuccess = loginService.login(userIdTF.getText(), passwordTF.getText());
                if(isSuccess){
                    LoginDialog.this.setVisible(false);
                    JOptionPane.showMessageDialog(root, "登陆成功!");
                    isOK = true;
                }else{
                    JOptionPane.showMessageDialog(root, "用户名或密码错误");
                }
            }
        });
        this.setVisible(true);
    }

    public boolean isOk(){
        return isOK;
    }
}
